package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.entity.vo.ReturnListCountVo;
import com.atguigu.jxc.service.ReturnListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 22:54 星期日
 * description:
 */
@Service
public class ReturnListServiceImpl implements ReturnListService {

    @Autowired
    ReturnListDao returnListDao;

    @Autowired
    ReturnListGoodsDao returnListGoodsDao;

    /**
     * 退货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * @param returnNumber
     * @param supplierId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String returnNumber,
                                    Integer supplierId,
                                    Integer state,
                                    String sTime,
                                    String eTime) {
        Map<String,Object> map = new HashMap<>();

        List<ReturnList> returnLists = returnListDao.list(returnNumber,supplierId,state,sTime,eTime);
        map.put("rows",returnLists);
        return map;
    }

    /**
     * 删除退货单
     * @param returnListId
     * @return
     */
    @Override
    public ServiceVO delete(Integer returnListId) {

        //判断该退货单下是否有退货数据
        List<ReturnListGoods> list = new ArrayList<>();
        list = returnListGoodsDao.ifHasReturnListGoods(returnListId);
        if (list.size() > 0){
            return new ServiceVO(ErrorCode.GOODS_TYPE_ERROR_CODE,ErrorCode.GOODS_TYPE_ERROR_MESS);
        }

        returnListDao.delete(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 退货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @Override
    public String count(String sTime,
                        String eTime,
                        Integer goodsTypeId,
                        String codeOrName) {
        List<ReturnListCountVo> returnListCountVoList =
                returnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        String s = JSON.toJSONString(returnListCountVoList);
        return s;
    }
}
